import { Switch, Route, Redirect } from "react-router";
import { userinfo } from '../utils/getUserInfo'

function RouterView(props) {
    const { routes } = props
    return <Switch>
        {
            routes.map((item, index) => {
                //登录校验
                //是否需要登录检验
                if (item.meta.mustLogin) {
                    //是否登录
                    if (!userinfo().token) {
                        return <Route key={index} exact={item.exact} path={item.path}>
                            <Redirect to='/login' />
                        </Route>
                    }//没有登录
                }

                //循环路由：Route除了定义组件之外，还起到占位作用
                if (item.component) {
                    return <Route key={index} exact={item.exact} path={item.path} component={item.component} />
                } else {
                    return <Route key={index} exact={item.exact} path={item.path}>
                        <Redirect to={item.redirect} />
                    </Route>
                }
            })
        }
    </Switch>
}

export default RouterView